angular.module('mamApp').controller('albumCreateController', ['$scope', '$state', '$uibModal', 'FileUploader', 'api', 'AlbumManager', 'ChannelManager', 'BaseValidations','ImsErr',
    function ($scope, $state, $uibModal, FileUploader, api, AlbumManager, ChannelManager, BaseValidations,ImsErr) {
        $scope.BaseValidations = BaseValidations;
        $scope.image = false;
        $scope.disabled = false;
        $scope.image_a = false;
        $scope.formData = {updateTimes: 71, programSort: 61};
        $scope.commonTagsSelected = [];
        $scope.specialTagsSelected = [];

        ChannelManager.categoriesList().then(function (result) {
            $scope.categories = result.data.list;
        })


        $scope.categoryChange = function (category) {
            $scope.formData.categoryId = category.categoryId;
            $scope.channelSelected = {};
            ChannelManager.channelsList(category.categoryId).then(function (result) {
                $scope.channels = result.data.list;
            })
        };


        $scope.channelChange = function (channel) {
            $scope.specialTagsSelected = [];
            $scope.formData.channelId = channel.channelId;
            $scope.specialTagsList(channel.channelId);

        }

        ChannelManager.commTagsList().then(function (result) {
            $scope.commonTags = [];
            result.data.list.forEach(function (item) {
                if (item.hasOwnProperty("list")) {
                    item.list.forEach(function (tag) {
                        $scope.commonTags.push({
                            commtagId: tag.commtagId,
                            dimensionsId: tag.dimensionsId,
                            commtagName: tag.commtagName,
                            dimensionsName: item.dimensionsName
                        })
                    })
                }

            })
        })


        $scope.specialTagsList = function (channelId) {
            $scope.specialTags = [];
            ChannelManager.specialTagsList(channelId).then(function (result) {
                result.data.list.forEach(function (item) {
                    if (item.hasOwnProperty("list")) {
                        item.list.forEach(function (tag) {
                            $scope.specialTags.push({
                                specialId: tag.specialId,
                                dimensionsId: tag.dimensionsId,
                                specialName: tag.specialName,
                                dimensionsName: item.dimensionsName
                            })
                        })
                    }

                })

            })
        }


        $scope.list = [];
        $scope.uploaderAudio = new FileUploader({
            alias: "uploadFile",
            queueLimit: 20,//文件个数
            filters: [{
                name: 'uploadFile',//后台接收字段
                fn: function (item /*{File|FileLikeObject}*/, options) {
                    if (!/\.(mp3|MP3|AAC|aac)$/.test(item.name)) {
                        $scope.imageMsg = "最多只能上传" + 20 + "条mp3或者aac格式且每个小于" + 150 + "MB的音频";
                        $scope.image_a = true;
                        return false;
                    }
                    else if (item.size > 150 * 1024 * 1024 || item.size < 0.5 * 1024 * 1024) {
                        $scope.imageMsg = "最多只能上传" + 20 + "条mp3或者aac格式且每个小于" + 150 + "MB的音频";
                        $scope.image_a = true;
                        return false;
                    } else {
                        $scope.imageMsg = ""
                        $scope.image_a = false;
                        return true;
                    }

                }
            }],
            onSuccessItem: function (fileItem, response, headers) {
                if (response.c == "s") {
                    delete  response.c;
                    fileItem.webUrl = response.fileVO.webUrl;
                    $scope.list.push({file: response.fileVO, audio: response.audioInfoVO});
                } else {
                    fileItem.isSuccess = 'uploadFail';
                }
            },
            url: api + "/cp/programs/audio",
            headers: {'X-Auth-Token': $scope.userInfo.token}
        });

        $scope.uploadAllAudio = function () {
            $scope.uploaderAudio.uploadAll();
        };
        $scope.removeFile = function ($index, item) {
            $scope.list.forEach(function (file) {
                if (file.file.webUrl == item.webUrl) {
                    $scope.list.splice($scope.list.indexOf(file), 1);
                }
            })
        }


        $scope.myImage = '';
        $scope.myCroppedImage = '';

        var handleFileSelect = function (evt) {
            var file = evt.currentTarget.files[0];

            $scope.uploadCover(file);

        };

        setTimeout(function () {
            angular.element(document.querySelector('#fileInput')).on('change', handleFileSelect);
        }, 1000)
        $scope.uploadCover = function (image) {
           if(image.type.indexOf("image")==-1){
             $scope.$apply($scope.errorImg='仅支持PNG/JPG/JPEG格式');
               return false;
           }else {
               $scope.$apply($scope.errorImg='');
           }

            var modalInstance = $uibModal.open({
                animation: true,
                backdrop:false,
                templateUrl: 'uploadCover.html',
                controller: 'coverManageController',
                resolve: {
                    params: function () {
                        return {image: image,coverType:"album"};
                    }
                }
            });
            modalInstance.result.then(function (result) {
                $("#fileInput").prop("value","");

                if (result.url) {
                    $scope.formData.albumCover = result.url;

                }

            })
        }


        $scope.submit = function () {
            $scope.disabled = true;
            var commTag = [];
            var specialTag = [];
            $scope.commonTagsSelected.forEach(function (item) {
                commTag.push(item.commtagId)
            })
            $scope.specialTagsSelected.forEach(function (item) {
                specialTag.push(item.specialId)
            })
            $scope.formData.commTag = commTag.join(",")
            $scope.formData.specialTag = specialTag.join(",")
            $scope.formData.list = $scope.list;
            $scope.formData.keyword = $scope.formData.keyword ? $scope.formData.keyword.split(",") : [];
            AlbumManager.albumAdd($scope.formData).then(function (result) {
                $scope.disabled = false;
                if (result.data.c == "s") {

                    $state.go("main.album", {}, {reload: true})
                }else {
                    $scope.formData.keyword = $scope.formData.keyword.join(",")
                    ImsErr.open({message:result.data.i}).then(function (a) {
                    })
                }
            })
        }

        $scope.cancel = function () {
            $state.go('main.album', {}, {reload: true})
        }


    }])

